<template>
  <div class="upload-img">
    <el-upload
      class="avatar-uploader"
      action=""
      :show-file-list="false"
      :http-request="uploadImg"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <el-progress
      v-if="percent>0&&percent<100"
      :text-inside="true"
      :stroke-width="26"
      :percentage="percent"
    />
  </div>
</template>
<script>
var COS = require('cos-js-sdk-v5')

// SECRETID 和 SECRETKEY请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
var cos = new COS({
  SecretId: 'AKIDwpY6SfliXAcx66xeCaKvOflgRzMWUKIB',
  SecretKey: 'pRr44qNowqVxBC2e2CBwRjlcVM1RehYA'
})
export default {
  data() {
    return {
      imageUrl: '',
      percent: 0
    }
  },
  methods: {
    uploadImg(data) {
    //   console.log(27, data)
      const that = this
      cos.uploadFile({
        Bucket: 'hr-zp-1315674534', /* 填写自己的bucket，必须字段 */
        Region: 'ap-nanjing', /* 存储桶所在地域，必须字段 */
        Key: Date.now().toString(), /* 存储在桶里的对象键（例如:1.jpg，a/b/test.txt，图片.jpg）支持中文，必须字段 */
        Body: data.file, // 上传文件对象
        SliceSize: 1024 * 1024 * 5, /* 触发分块上传的阈值，超过5MB使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */
        onProgress: function(progressData) {
        //   console.log(JSON.stringify(progressData))
          console.log(46, progressData)
          that.percent = progressData.percent * 100
        }
      }, (err, data) => {
        if (err) {
          console.log('上传失败', err)
        } else {
          console.log('上传成功', data)
          this.imageUrl = 'http://' + data.Location
        }
      })
    }
  }
}
</script>
<style scoped lang="scss" >
.avatar-uploader,.upload-img{
    display: inline-block;
}
 ::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  ::v-deep .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  ::v-deep .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  ::v-deep .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
